package com.loveyukari.service;

import com.loveyukari.utils.HttpUtils;
import net.sf.json.JSONObject;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.concurrent.TimeUnit;

@Service
public class WeixinService {
    @Autowired
    private RedisService redisService;



    public String getAccessToken() throws Exception {
        String accessToken = redisService.get("weixin:access:token", String.class);
        if (StringUtils.isBlank(accessToken)){
            String resultJson = HttpUtils.sendGet("https://api.weixin.qq.com/cgi-bin/token?grant_type=client_credential&appid=wxa9d21e27978edd82&secret=b4ed3f8ae73c1a57436a431820abaf94");
            JSONObject json = JSONObject.fromObject(resultJson);
            accessToken = json.optString("access_token");
            int expiresIn = json.optInt("expires_in");
            redisService.set("weixin:access:token",accessToken,expiresIn, TimeUnit.SECONDS);
        }
        return accessToken;
    }




}
